#ifndef HTTPREQUEST_H
#define HTTPREQUEST_H

#include "request.h"

namespace Wishmaster {

	class HttpRequest;
	typedef QSharedPointer<HttpRequest> HttpRequestPtr;
    class WISHMASTER_EXPORT HttpRequest : public Request
	{
		Q_OBJECT
	public:
		explicit HttpRequest(HttpConnector* parent, const QUrl& url, const QString& type = "GET");

		virtual void perform();
		virtual bool error();

		QString host() const;
		QString path() const;

		bool redirect() { return m_redirect; }

		int getStatusCode();

		void setReferer(const QString& referer);

		virtual QString contentType() const;

		void clearPostData();
		void addPostField(const QString& fieldname, const QString& fielddata);
		void addPostFile(const QString& fieldname, const QString& filename, const QByteArray& filedata);

		QString cookie(const QString& key);

	public slots:
		virtual void cancel();

	signals:
		void result(RequestPtr r, const QByteArray& arr);
		void progress(int done, int total);

	protected slots:
		void slot_progress(qint64 done, qint64 total);
		void slot_result();

	protected:
		virtual void timerEvent(QTimerEvent * event);
		void parseSetCookie(const QString& cookie);

	private:
		static const int RecvTimeout = 30000; // TODO make customizable
		static const int Retries = 3; // TODO make customizable

		int m_timerID;
		int m_triesCount;
		bool m_error;
		HttpConnector* m_parent;
		QUrl m_url;
		QString m_requestType;
		QByteArray m_data;
		QString m_referer;
		QString m_userAgent;
		bool m_redirect;
		QMap<QString, QString> m_cookieMap;
		int m_progressCounter;
		QNetworkReply* m_reply;
		QString m_contentType;
	};

} // namespace Wishmaster

#endif // HTTPREQUEST_H
